﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace InfonavaEye.components
{
    public partial class UniNumberBox : UserControl
    {

        private int step = 1;

        private int number = 0;

        /// <summary>
        /// 当前值
        /// </summary>
        [Browsable(true), Category("数据"), Description("当前值")]
        public int Value
        {
            get
            {
                return number;
            }
            set
            {
                number = value;
                this.checkValue();
            }
        }

        /// <summary>
        /// 最小值
        /// </summary>
        [Browsable(true), Category("数据"), Description("最小值")]
        public int Min
        {
            get; set;
        }

        /// <summary>
        /// 最大值
        /// </summary>
        [Browsable(true), Category("数据"), Description("最大值")]
        public int Max
        {
            get; set;
        }

        /// <summary>
        /// 步长
        /// </summary>
        [Browsable(true), Category("数据"), Description("步长")]
        public int Step {
            get {
                return step;
            }
            set {
                step = value;
            }
        }

        private bool enabled = true;
        /// <summary>
        /// 指示是否已启用该控件，如果要使用原有的Enabled禁用控件，需要设置EnabledSet达到目的，EnabledSet级别高于此属性级别
        /// </summary>
        [Browsable(true), Category("行为"), Description("指示是否已启用该控件")]
        public new bool Enabled
        {
            get { return enabled; }
            set
            {
                enabled = value;
                this.btnMinus.Enabled = enabled;
                this.btnPlus.Enabled = enabled;
                this.txtNumber.Enabled = enabled;
                base.Enabled = true;
            }
        }

        public delegate void ValueChangedDelegate(object sender, EventArgs e);
        public event ValueChangedDelegate valueChanged;

        public UniNumberBox()
        {
            InitializeComponent();
            this.checkValue();
        }

        private void btnMinus_Click(object sender, EventArgs e)
        {
            this.Value -= this.Step;
            this.checkValue();
        }

        private void txtNumber_TextChanged(object sender, EventArgs e)
        {
            this.Value = int.Parse(this.txtNumber.Text);
            this.checkValue();
        }

        private void btnPlus_Click(object sender, EventArgs e)
        {
            this.Value += this.Step;
            this.checkValue();
        }

        private void txtNumber_KeyPress(object sender, KeyPressEventArgs e)
        {
            //如果输入的不是退格和数字，则屏蔽输入
            if (!(e.KeyChar == 8 || (e.KeyChar >= 48 && e.KeyChar <= 57)))
            {
                e.Handled = true;
            }
        }

        private void UniNumberBox_Load(object sender, EventArgs e)
        {
           
        }

        private void checkValue(){
            if (this.Value < this.Min)
            {
                this.Value = this.Min;
            }
            if (this.Value > this.Max)
            {
                this.Value = this.Max;
            }
            this.txtNumber.Text = this.Value.ToString();
            if (this.valueChanged != null) {
                this.valueChanged(this,null);
            }
        }
    }
}
